<?php
/**
 * Data model
 *
 * @category  DataModel
 * @package   DataModel
 * @author    DataModel Generator v1.0
 * @version   $Revision: 127 $
 */
/**
 * Data model for table "project"
 *
 * @category  DataModel
 * @package   DataModel
 * @author    DataModel Generator v1.0
 * @version   $Revision: 127 $
 */

class Project extends BaseProject
{
    public static function getSvnPath()
    {
    
    }

    public static function isAvailableProject($alias)
    {
        if (file_exists(PROJECTS_DIR . '/' . $alias)) {
            throw new Exception('Project already exsists');
        }
        $project = Project::getByParams(array('name' => $alias));
        if ($project) {
            throw new Exception('Project already exsists');
        }
        using('Site.Code.Repository.SVN');
        SVNRepository::isAvailableRepository();
    }

    public function create()
    {
        if (!$this->name) {
            throw new Exception('Enter name for project');
        }
        self::isAvailableProject($this->name);

        if (!$this->title) {
            throw new Exception('Enter title for project');
        }
        $path = PROJECTS_DIR . '/' . $this->name;
        mkdir($path);
        $this->path = realpath($path);

        if ($this->repository_type == 'svnl') {
            $repPath = '/var/svn-repos/svn/' . $this->name;
            SVNRepository::createRepository($repPath);
            SVNRepository::createStandardDirectories($repPath);
            $this->repository_path = $repPath . '/trunk';
        }
        if ($this->repository_type == 'svnl' || $this->repository_type == 'svnr') {
            SVNRepository::checkOut($this->repository_path, $this->path);
        }
        $this->save();
    }

    public function findByWords(array $words)
    {
        $q = ORMQuery::selectBuilder()
            ->from('Project p');
        foreach($words as $word) {
            $word = trim($word);
            $q ->orWhere('p.name LIKE ?','%' . $word . '%');
            $q ->orWhere('p.title LIKE ?','%' . $word . '%');
            $q ->orWhere('p.description LIKE ?','%' . $word . '%');
        }
        return $q->fetchAll('Project');
    }

    public function getBuildDir()
    {
        return realpath( SITE_DIR.'/../builds/'.$this->name );
    }

    public function getDocsDir()
    {
        return $this->getBuildDir() . DOCS_DIR;
    }

    public function getCoverageDir()
    {
        return $this->getBuildDir() . COVARAGE_DIR;
    }

    public function getCurrentTask()
    {
        $q = ORMQuery::selectBuilder()
            ->from('Scheduler f')
            ->andWhere('f.project_id = ?', $this->id)
            ->orderBy('id desc')
            ->limit(1);
        return $q->fetch('Scheduler');
    }
    
    public static function search(array $words)
    {
        $res = array();        
        $models = array();
        $models[] = 'Pfolder';
        $models[] = 'Pfile';
        $models[] = 'Doctype';
        $models[] = 'Doctags';
        $models[] = 'Phpclass';
        $models[] = 'DocPackage';

        foreach($models as &$model) {
            $modelObj = new $model();
            $res = array_merge( $res, $modelObj->findByWords($words) );
        }
        
        return $res;
    }

    public static function saveIfNotExists($params = null)
    {
        return parent::saveIfNotExists($params, __CLASS__);
    }
}
